home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Crossword / Source / FunctionCache.h < prev    next >
Text File  |  1995-06-12  |  2KB  |  53 lines

  1. /*
  2.  
  3. File FunctionCache.h
  4.  
  5. A cache remembers function values, f(x) = y, so that function evaluations can avoid lengthy recomputations.  Each cache has a finite capacity.  When a new value is added to the cache, the cache discards the value that has gone unused for the longest period of time.  It uses a callback function to alert the application that a value is being discarded.  In this way memory can be freed and reclaimed for other uses.
  6.  
  7. */
  8.  
  9. #import <objc/Object.h>
  10. #import <objc/HashTable.h>
  11.  
  12.  
  13. /* ————————————————————————————————————————————————————————————————————————————  */
  14.  
  15.  
  16. #define ISASTRING        0
  17. #define ISAOBJECT        1
  18.  
  19. typedef void    (* freeFunction)(void *);
  20.  
  21. typedef struct cacheElement {
  22.                                 void                    * key, * value;
  23.                                 struct cacheElement        * previous, * next;
  24.                                 int                        utility;
  25.     } cacheElement;
  26.  
  27.  
  28. /* ————————————————————————————————————————————————————————————————————————————  */
  29.  
  30.  
  31. @interface FunctionCache:Object
  32. {
  33.     HashTable        * cache;
  34.     cacheElement    * worst, * best;
  35.     unsigned        capacity;
  36.     freeFunction    freeKey, freeValue;
  37. }
  38.  
  39. - initKey: (const char *) key
  40.         value: (const char *) value
  41.         capacity: (unsigned) theCapacity
  42.         freeKey: (freeFunction) theFreeKey
  43.         freeValue: (freeFunction) theFreeValue;
  44.  
  45. - initKeyType: (int) key  valueType: (int) value  capacity: (unsigned) theCapacity;
  46. - setCapacity: (unsigned) theCapacity;
  47. - add: (void *) key  value: (void *) value;
  48. - remove: (void *) key;
  49. - (void *) find: (void *) key;
  50. - (cacheElement *) detach: (cacheElement *) cell;
  51. - release: (cacheElement *) cell;
  52.  
  53. @end